package com.ace_server.controller;

import com.ace_server.model.Demo;
import com.ace_server.service.DemoService;
import com.solution.framework.controller.BaseController;
import com.solution.framework.util.MessageDefined;
import com.solution.framework.util.MessageDto;
import com.solution.framework.util.Pagination;
import com.solution.framework.util.RequestVarsUtil;
import org.hibernate.criterion.Criterion;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created with generator tools.
 * Demo: sunguangzhu
 * DateTime:2014-02-19 05:00:53
 */
@Controller
public class DemoController extends BaseController<Demo> {
    private static final String path_index = "demo/demo_index";
    private static final String path_input = "demo/demo_input";
    private static final String path_view = "demo/demo_view";

    @Resource
    private DemoService demoService;

    @RequestMapping
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("demo index method ....");
        Pagination pagination = RequestVarsUtil.getPagination(request);
        List<Criterion> criterions = this.getSearchCriterions(request);
        List<Demo> entitys = this.demoService.listByCriteria(pagination, criterions);
        int total = this.demoService.getTotalByCriteria(criterions);
        RequestVarsUtil.setPagination(pagination, total);
        request.setAttribute("pageModel", pagination);
        model.addAttribute("list", entitys);
        return path_index;
    }

    @RequestMapping
    public String edit(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("edit method ....");
        Demo demo = this.demoService.get(id);
        model.addAttribute("demo", demo);
        return path_input;
    }

    @RequestMapping
    public String add(Long bizId, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("add method ....");
        Demo demo = new Demo();
        model.addAttribute("demo", demo);
        return path_input;
    }

    @RequestMapping
    public String view(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("view method ....");
        Demo demo = this.demoService.get(id);
        model.addAttribute("demo", demo);
        return path_view;
    }

    @ResponseBody
    @RequestMapping
    public void save(Demo demo, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("save method ....");
        MessageDto messageDto = new MessageDto();
        try {
            if (demo.getId() != null) {
                this.demoService.update(demo);
                super.setMessageDto(messageDto, MessageDefined.update_success_info);
            } else {
                this.demoService.save(demo);
                super.setMessageDto(messageDto, MessageDefined.save_success_info);
            }
        } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
            e.printStackTrace();
        }
        super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void delete(String ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        MessageDto messageDto = new MessageDto();
        try {
            Integer count = this.demoService.delete(ids);
            super.setMessageDto(messageDto, MessageDefined.del_success_info);
        } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
            e.printStackTrace();
        }
        super.writeToClient(response, messageDto.toString());
    }

}
